package com.example.canteen;

import android.annotation.SuppressLint;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;

public class repeatBtn {
    private Button mImageView;

    public repeatBtn(Button mImageView) {
        this.mImageView = mImageView;
    }

    @SuppressLint("ClickableViewAccessibility")
    public void showRepeatBtn() {
        mImageView.setOnTouchListener((v, event) -> {
            // 触摸时取消动画，并缩小，有按下的感觉
            if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
                mImageView.setScaleX(0.9f);
                mImageView.setScaleY(0.9f);
                if (mImageView.getAnimation() != null) {
                    mImageView.getAnimation().cancel();
                }
            } else { // 松手后，恢复大小，并继续呼吸效果
                mImageView.setScaleX(1.0f);
                mImageView.setScaleY(1.0f);
                if (mImageView.getAnimation() != null) {
                    mImageView.getAnimation().start();
                }
            }

            return false;
        });
        // 放大小时view，完全显示后开始呼吸效果
        ScaleAnimation enterAnim = new ScaleAnimation(0f, 0.9f, 0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        enterAnim.setDuration(300); // 默认只执行一遍
        enterAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) { }
            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                ScaleAnimation anim = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setDuration(750);
                anim.setRepeatMode(Animation.REVERSE); // 放大并缩小，时间为750*2
                anim.setRepeatCount(Animation.INFINITE); // 无限循环
                mImageView.setAnimation(anim);
                mImageView.startAnimation(mImageView.getAnimation());
            }
        });
        mImageView.startAnimation(enterAnim);
    }

    /**
     * 动画隐藏连发按钮
     */
    private void hideRepeatBtn() {
        // 缩小退出
        ScaleAnimation anim = new ScaleAnimation(1.1f, 0f, 1.1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(300);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) { }
            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (mImageView.getAnimation() != null) {
                    mImageView.getAnimation().cancel();
                }
            }
        });
        mImageView.startAnimation(anim);
    }

}
